// import shop from '../../assets/js/shop';
import urlConfig from "@/assets/login/config";
import comApiList from "@/api/commonApi.js";
import { getTheadShow } from "@/assets/js/com";
import { localGet } from '@/utils'

// initial state
// shape: [{ id, quantity }]
const state = () => ({
  //上传市目标文件
  cityUploadUrl: urlConfig.cityUploadUrl,
  //拉取市目标报送信息
  cityMessageUrl: urlConfig.cityMessageUrl,
  cityGoalUrl: urlConfig.cityGoalUrl,
  // 打包前缀名称
  platform: "hxmb/biz",
  // 退出地址
  logoutUrl: urlConfig.sso_logout_url,
  enumeration: {},
  defaultUploadObj: {
    headers: {
      token: "test"
    },
    action: "/api/authority/attachment/upload"
  },
  // 是否党建园地
  isPartyGarden: false,
  // 用户信息
  userInfo: {
    userid: "",
    name: ""
  },
  authButton: [],
  // 动态表格表头数据处理初始值 (辅助控制动态表格的按钮状态)
  arr: getTheadShow(),
  // 按钮权限列表
  btnList: [],
  // 当前未读消息数
  unReadCount: 0,
  // 是否显示加载层
  hasLoading: false,
  pageHasLoading: false,
  errortime: 0
})

// getters
const getters = {
  // cartProducts: (state, getters, rootState) => {
  //   return state.items.map(({ id, quantity }) => {
  //     const product = rootState.products.all.find(product => product.id === id)
  //     return {
  //       title: product.title,
  //       price: product.price,
  //       quantity
  //     }
  //   })
  // },

  // cartTotalPrice: (state, getters) => {
  //   return getters.cartProducts.reduce((total, product) => {
  //     return total + product.price * product.quantity
  //   }, 0)
  // }
}

// actions
const actions = {
  async getAuthButton({ commit, state }) {
    if (localGet("userinfo").logined) {
      const res = await comApiList.getAuthMenu({
        type: "OPT",
        appCode: "hxmb",
        accountId: state.userInfo ? state.userInfo.userid : ""
      });
      if (res.data.code === 0) {
        commit("getAuthButtons", res.data.data);
        localStorage.setItem("AuthButtons", JSON.stringify(res.data.data));
      } else {
        console.log(res.data.errmsg);
      }
    }
  }
}

// mutations
const mutations = {
  GETUSERINFO(state) {
    // 解析所有的token到此store下面。
    state.year = localStorage.getItem("year") || new Date().getFullYear() + "";
    state.userInfo = localGet("userinfo");
    if (state.userInfo) {
      state.userInfo.userId = state.userInfo.userid;
    }
    state.defaultUploadObj.headers.token = state.userInfo ? state.userInfo.token : "test";
  },
  getAuthButtons(state, params) {
    if (localStorage.getItem("AuthButtons")) {
      state.authButton = JSON.parse(localStorage.getItem("AuthButtons"));
    } else {
      state.authButton = params;
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}